// pages/stateSearch/stateSearch.js
let app = getApp();
var api = app.globalData.api
var util = app.globalData.utils
var address = app.globalData.address
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '组合搜索', //导航栏 中间的标题
      hidden: 1
    },
    height: app.globalData.height * 2 + 20, // 此页面 页面内容距最顶部的距离
    filterArray: {},
    searchBackHeight: 0,
    currentPage: 0,
    locationList: [],
    isLoading: 0,
    isBackHidden: false,
    listBackHeight: 0,
    dataYear: {},
    dataAddress: {},
    dataTeam: {},
    dataLabel: {},
    dataPartner: {},
    dataProvince: {},
    isSelect: -1,
    isSelectList: -1,
    searchBackTop: (60 + (app.globalData.height * 2 + 20)) * 2,
    str: '',
    strImg: '',
    address: '', //详细收货地址（四级）
    value: [0, 0, 0], // 地址选择器省市区 暂存 currentIndex
    region: '', //所在地区
    regionValue: [0, 0, 0], // 地址选择器省市区 最终 currentIndex
    provinces: [], // 一级地址
    citys: [], // 二级地址
    areas: [], // 三级地址
    visible: false,
    addressId: '', // 数据库内地址编号
    type: 1,
    province_name: '请选择',
    city_name: '',
    provinceNum: -1,
    num: 0,
    district_name: '',
    paramsData: {},
    isValueShow: false
  },
  confirmSubmit(e){
    let that = this;
    let value = e.detail.value.searchText;
    let type = e.currentTarget.dataset.isselect;
    console.log(value)
    if (type == 1) {
      that.data.paramsData.start_time = value
      that.data.dataYear = {
        samplingDate: value,
        isValueShow: true,
      }
      that.setData({
        dataYear: that.data.dataYear,
        paramsData: that.data.paramsData
      })
    } else if (type == 2) {
      that.data.paramsData.province = value
      that.data.dataProvince = {
        province: value,
        isValueShow: true,
      }
      that.setData({
        dataProvince: that.data.dataProvince,
        paramsData: that.data.paramsData
      })
    } else if (type == 3) {
      that.data.paramsData.title = value
      that.data.dataTeam = {
        teamName: value,
        isValueShow: true,
      }
      that.setData({
        dataTeam: that.data.dataTeam,
        paramsData: that.data.paramsData
      })
    } else if (type == 4) {
      that.data.paramsData.shibboleth = value
      that.data.dataLabel = {
        tagName: value,
        isValueShow: true,
      }
      that.setData({
        dataLabel: that.data.dataLabel,
        paramsData: that.data.paramsData
      })
    }
    that.setData({
      searchBackHeight: 0,
      searchValue: '',
    })
  },
  onDelete(e) {
    let that = this;
    let type = e.currentTarget.dataset.type
    if (type == 1) {
      delete that.data.paramsData.start_time
      that.setData({
        dataYear: {},
        paramsData: that.data.paramsData
      })
    } else if (type == 2) {
      // 默认联动显示北京
      delete that.data.paramsData.province
      that.setData({
        dataProvince: {},
        paramsData: that.data.paramsData
      })
    } else if (type == 3) {
      delete that.data.paramsData.title
      that.setData({
        dataTeam: {},
        paramsData: that.data.paramsData
      })
    } else if (type == 4) {
      delete that.data.paramsData.shibboleth
      that.setData({
        dataLabel: {},
        paramsData: that.data.paramsData
      })
    }
  },
  onLocation(e) {
    let that = this
    let id = e.currentTarget.dataset.id
    let index = e.currentTarget.dataset.index
    that.setData({
      isLocation: index
    })
    wx.navigateTo({
      url: '/pages/Activity/pages/activityInfo/index?id=' + e.currentTarget.dataset.id,
    })
  },
  // 搜索
  confirmTap: function(event) {
    let that = this;
    let type = event.currentTarget.dataset.isselect
    let text = ''
    if (event.detail.value == undefined && that.data.dataAddress.areaName != undefined) {
      text = that.data.dataAddress.areaName.replace(/ /g, ",");
    } else {
      text = event.detail.value.searchText.replace(/ /g, ",");
    }
    if (type == 2) {
      that.data.paramsData.areaName = text
      that.data.dataAddress = {
        areaName: text,
        isValueShow: true,
      }
      that.setData({
        dataAddress: that.data.dataAddress,
        paramsData: that.data.paramsData,
        searchBackHeight: 0,
      })
    }
  },
  onSelectList(e) {
    let that = this;
    let value = e.currentTarget.dataset.value;
    let item = e.currentTarget.dataset.item
    let type = e.currentTarget.dataset.type
    let height = wx.getSystemInfoSync().windowHeight
    if (type == 1) {
      that.data.paramsData.start_time = item.start_time1
      that.data.dataYear = {
        samplingDate: item.start_time1,
        counts: item.actcount,
        isValueShow: true,
      }
      that.setData({
        dataYear: that.data.dataYear,
        paramsData: that.data.paramsData
      })
    } else if (type == 2) {
      that.data.paramsData.province = item.province
      that.data.dataProvince = {
        province: item.province,
        counts: item.provincecount,
        isValueShow: true,
      }
      that.setData({
        dataProvince: that.data.dataProvince,
        paramsData: that.data.paramsData
      })
    } else if (type == 3) {
      that.data.paramsData.title = item.title
      that.data.dataTeam = {
        teamName: item.title,
        isValueShow: true,
      }
      that.setData({
        dataTeam: that.data.dataTeam,
        paramsData: that.data.paramsData
      })
    } else if (type == 4) {
      that.data.paramsData.shibboleth = item.shibboleth
      that.data.dataLabel = {
        tagName: item.shibboleth,
        counts: item.actcount,
        isValueShow: true,
      }
      that.setData({
        dataLabel: that.data.dataLabel,
        paramsData: that.data.paramsData
      })
    }
    that.setData({
      searchBackHeight: 0,
    })
  },
  cityChange2(e) {
    let {
      provinces
    } = this.data
    let cityNum = e.currentTarget.dataset.id
    this.setData({
      city_name: e.currentTarget.dataset.name,
      cityNum: cityNum,
      type: e.currentTarget.dataset.type,
      district_name: "请选择",
      areas: address.areas[e.currentTarget.dataset.id]
    })
  },
  cityChange3(e) {
    this.setData({
      district_name: e.currentTarget.dataset.name,
      type: e.currentTarget.dataset.type,
    })
  },
  cityChangTap(e) {
    var value = e.detail.value
    if (e.currentTarget.dataset.type == 1) {
      this.setData({
        city_name: "请选择",
        district_name: "",
        type: e.currentTarget.dataset.type,
      })
    } else if (e.currentTarget.dataset.type == 2) {
      this.setData({
        district_name: "",
        type: e.currentTarget.dataset.type,
      })
    }

  },
  // 处理省市县联动逻辑 并保存 value
  cityChange(e) {
    var value = e.detail.value
    this.setData({
      province_name: e.currentTarget.dataset.name,
      provinceNum: e.currentTarget.dataset.id,
      type: e.currentTarget.dataset.type,
      city_name: "请选择",
      district_name: "",
      citys: address.citys[e.currentTarget.dataset.id]
    })
  },
  close() {
    return
  },
  onSeachBack() {
    this.setData({
      searchBackHeight: 0,
      isBackHidden: false,
      isSelectList: -1,
      locationList: [],
      currentPage: 0,
      isLoading: false,
      dataItem: {},
      searchText: ''
    })
  },
  onSeach(e) {
    let that = this;
    let type = e.currentTarget.dataset.type
    let height = wx.getSystemInfoSync().windowHeight
    let strText = ''
    let strImg = ''
    if (type == 1) {
      strText = '请选择月份或输入日期'
      that.setData({
        searchValue: util.formatData(new Date),
      })
    } else if (type == 2) {
      strText = '请选择或输入省'
      that.setData({
        searchValue: '',
      })
      // 默认联动显示北京
    } else if (type == 3) {
      strText = '请选择或输入活动名称'
      that.setData({
        searchValue: '',
      })
      // 默认联动显示北京
    } else if (type == 4) {
      strText = '请选择或输入机构名称'
      that.setData({
        searchValue: '',
      })
      // 默认联动显示北京
    }
    that.setData({
      searchBackHeight: (height * 2) - 30,
      isBackHidden: true,
      isSelect: type,
      str: strText,
    })
  },
  bindsubmit(e) {
    let that = this;
    // wx.showLoading({
    //   icon: 'none',
    //   title: '正在加载中',
    // })
    that.setData({
      isLoading: 0,
    })
    that.onMarkers(that.data.paramsData)
  },

  bindreset() {
    let that = this;
    this.setData({
      dataYear: {},
      dataAddress: {},
      dataTeam: {},
      dataLabel: {},
      dataPartner: {},
      paramsData: {},
      searchValue: '',
      type: 1,
      dataProvince:{},
      locationList: [],
      listBackHeight: 0,
      currentPage: 0,
      isLoading: false,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.onTag();
  },
  onTag() {
    let that = this;
    var url = app.u.H + app.u.river.GETPROVINCE
    api.requestUrl({}, url).then(res => {
      console.log(res)
      if (res.code == 200) {
        console.log(res)
        that.data.filterArray['provinceArr'] = res.data.provincelist
        that.data.filterArray['dateTime'] = res.data.monthcount
        that.data.filterArray['orgcount'] = res.data.orgcount
        that.data.filterArray['activityList'] = res.data.activityList
        that.setData({
          filterArray: that.data.filterArray,
        })
        console.log(that.data.filterArray)
      }
    })
  },
  onNavBack() {
    this.setData({
      listBackHeight: 0,
      currentPage: 0,
      isLoading: false,
      locationList: [],
    })
  },
  onMarkers(params) {
    let that = this;
    params.pages = that.data.currentPage + 1
    let height = wx.getSystemInfoSync().windowHeight
    var url = app.u.H + app.u.river.SEARCHAL
    console.log('params:', params)
    api.requestUrl(params, url).then(res => {
      console.log(res)
      if (res.code == 200) {
        // setTimeout(function() {
        //   wx.hideLoading()
        // }, 500)
        let data = res.data
        let list = data.list
        if (list.length > 0) {
          let isShow = data.pageCount > data.pages? 1 : -1
          let locationList = that.data.locationList.concat(list)
          that.setData({
            listBackHeight: (height * 2) - 30,
            locationList: locationList,
            currentPage: data.pages,
            isLoading: isShow,
            num: data.actCount
          })
        } else {
          that.setData({
            isLoading:-1
          })
         
        }
      }
    })
    // var url = app.u.url + app.u.index.SelectListVillagePage;
    // api.requestUrl(params, url).then(res => {
    //   setTimeout(function() {
    //     wx.hideLoading()
    //   }, 500)
    //   let data = res.data.page
    //   let list = data.content
    //   if (list.length > 0) {
    //     let isShow = data.totalPages > data.pageable.pageNumber + 1 ? 1 : -1
    //     let locationList = that.data.locationList.concat(list)
    //     that.setData({
    //       listBackHeight: (height * 2) - 30,
    //       locationList: locationList,
    //       currentPage: data.pageable.pageNumber + 1,
    //       isLoading: isShow,
    //       num: data.totalElements
    //     })
    //   } else {
    //     that.setData({
    //       listBackHeight: 0,
    //       locationList: [],
    //     })
    //     wx.showToast({
    //       title: '暂无数据',
    //       icon: 'success',
    //       duration: 2000
    //     })
    //   }
    // });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },



  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    console.log('jin')
  },

  // /**
  //  * 用户点击右上角分享
  //  */
  // onShareAppMessage: function() {

  // }
})